package com.zt.dao.sys.impl;

import java.util.ArrayList;
import java.util.List;

import com.zt.dao.base.impl.BaseJdbcSupport;
import com.zt.dao.sys.ISampleDao;
import com.zt.model.sys.Sample;

/**
 * DAO类 SampleDao
 * 
 * @author zhangzhw
 * 
 */
public class SampleDao extends BaseJdbcSupport implements ISampleDao {

	@Override
	public int delete(Sample s) throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append("DELETE FROM SAMPLE WHERE SAMPLE_ID = ?");
		String sql = sb.toString();

		return super.update(sql, new Object[] { s.getSampleId() });
	}

	@Override
	public int save(Sample s) throws Exception {

		StringBuffer sb = new StringBuffer();
		sb.append("INSERT INTO SAMPLE\n");
		sb.append("  (SAMPLE_ID,\n");
		sb.append("   SAMPLE_NAME,\n");
		sb.append("   SAMPLE_DATE)\n");
		sb.append("VALUES\n");
		sb.append("  (?,\n");
		sb.append("   ?,\n");
		sb.append("   ?)");

		String sql = sb.toString();

		List<Object> parm = new ArrayList<Object>();
		parm.add(s.getSampleId());
		parm.add(s.getSampleName());
		parm.add(s.getSampleDate());

		return super.update(sql, parm.toArray());

	}

	@Override
	public int saveOrUpdate(Sample s) throws Exception {

		String sql = "SELECT COUNT(*) FROM SAMPLE WHERE SAMPLE_ID = ?";
		long l = super.queryForLong(sql, new Object[] { s.getSampleId() });

		if (l < 1)
			return save(s);
		else
			return update(s);

	}

	@Override
	public int update(Sample s) throws Exception {

		StringBuffer sb = new StringBuffer();
		sb.append("UPDATE SAMPLE\n");
		sb.append("   SET SAMPLE_ID   = ?,\n");
		sb.append("       SAMPLE_NAME = ?,\n");
		sb.append("       SAMPLE_DATE = ?\n");
		sb.append(" WHERE SAMPLE_ID = ?");

		String sql = sb.toString();

		List<Object> parm = new ArrayList<Object>();
		parm.add(s.getSampleId());
		parm.add(s.getSampleName());
		parm.add(s.getSampleDate());
		parm.add(s.getSampleId());

		return super.update(sql, parm.toArray());

	}

}
